<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SalaryMonth;
use App\Admin\Repositories\UserMonthTarget;
use App\Admin\Services\ErpRepairSettlementService;
use App\Admin\Services\UserMonthTargetService;
use App\Models\Store;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SalaryMonthController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SalaryMonth::with(['store', 'department', 'position']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('store.title', admin_trans('user.fields.store_id'));
            $grid->column('department.title', admin_trans('user.fields.department_id'));
            $grid->column('position.title', admin_trans('user.fields.position_id'));
            $grid->column('name');
            $grid->column('basic_salary')->display(function ($basic_salary) {
                if ($this->is_probation_period == DEFAULT_TYPE_ONE) {
                    return $this->probation_period_salary."(试用期)";
                } else {
                    return $basic_salary;
                }
            });

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
                $filter->equal('store_id')
                    ->select(Store::all()->pluck('title', 'id'))
                    ->load('department_id', 'user-department');
                $filter->equal('department_id')->select()->load('position_id', 'user-position');
                $filter->equal('position_id')->select();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SalaryMonth(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('basic_salary')->as(function ($basic_salary) {
                if ($this->is_probation_period == DEFAULT_TYPE_ONE) {
                    return $this->probation_period_salary."(试用期)";
                } else {
                    return $basic_salary;
                }
            });
            $show->field('updated_at');

            $show->UserMonthTargets(function ($model) {
                $grid = new Grid(new UserMonthTarget);

                $grid->model()->where('user_id', $model->id);

                // 设置路由
                $grid->resource('user-month-target');

                $grid->id();
                $grid->target_date();
                $grid->updated_at();

                $grid->disableCreateButton();
                $grid->disableEditButton();
                $grid->disableViewButton();
                $grid->disableDeleteButton();

                $titles = [
                    'id' => 'ID',
                    'target_date' => admin_trans('position-month-salary.fields.target_date'),
                    'store_title' => admin_trans('position-month-salary.fields.store_title'),
                    'department_title' => admin_trans('position-month-salary.fields.department_title'),
                    'position_title' => admin_trans('position-month-salary.fields.position_title'),
                    'name' => admin_trans('position-month-salary.fields.user_id'),
                    'erp_meet_car_count' => admin_trans('user-month-target.fields.erp_meet_car_count'),
                    'meet_car_commission_coefficient' => admin_trans('position-month-salary.fields.meet_car_commission_coefficient'),
                    'erp_meet_car_commission' => admin_trans('user-month-target.fields.erp_meet_car_commission'),
                    'real_output_value' => admin_trans('user-month-target.fields.real_output_value'),
                    'output_value_commission_coefficient' => admin_trans('user-month-target.fields.output_value_commission_coefficient'),
                    'output_value_achievement_commission' => admin_trans('user-month-target.fields.output_value_achievement_commission'),
                    'output_value_target' => admin_trans('user-month-target.fields.output_value_target'),
                    'real_output_values' => admin_trans('user-month-target.fields.real_output_value'),
                    'output_value_reach' => admin_trans('user-month-target.fields.output_value_reach'),
                    'output_value_score' => admin_trans('user-month-target.fields.output_value_score'),
                    'real_maintenance_vehicle' => admin_trans('user-month-target.fields.real_maintenance_vehicle'),
                    'maintenance_vehicle_reach' => admin_trans('user-month-target.fields.maintenance_vehicle_reach'),
                    'maintenance_vehicle_score' => admin_trans('user-month-target.fields.maintenance_vehicle_score'),
                    'kpi_coefficient' => admin_trans('user-month-target.fields.kpi_coefficient'),
                    'user_month_award' => admin_trans('user-month-target.fields.user_month_award'),
                    'user_month_punish' => admin_trans('user-month-target.fields.user_month_punish'),
                    'subtotal_merit_pay' => admin_trans('user-month-target.fields.subtotal_merit_pay'),
                    'gross_profit_reach' => admin_trans('user-month-target.fields.gross_profit_reach'),
                    'should_merit_pay' => admin_trans('user-month-target.fields.should_merit_pay'),
                    'basic_salary' => admin_trans('user-month-target.fields.basic_salary'),
                    'total_salary' => admin_trans('user-month-target.fields.total_salary'),
                    'probation_period_salary' => admin_trans('user-month-target.fields.probation_period_salary')
                ];

                $grid->export($titles);

                $grid->export()->rows(function ($rows) {
                    foreach ($rows as $index => &$row) {
                        $row->store_title = $row->user->store->title;
                        $row->department_title = $row->user->department->title;
                        $row->position_title = $row->user->position->title;
                        $row->name = $row->user->name;
                        $row->erp_meet_car_count = ErpRepairSettlementService::erpMeetCarCount([
                            'user_erp_id' => $row->user->erp_id,
                            'target_date' => $row->target_date,
                            'store_erp_id' => $row->user->store->erp_id
                        ]);

                        if ($row->positionMonthTarget->is_commission_coefficient_status == DEFAULT_STATUS_SUCCESS) {
                            $row->meet_car_commission_coefficient = $row->positionMonthTarget->meet_car_commission_coefficient;
                        }

                        $row->erp_meet_car_commission = $row->erp_meet_car_count * $row->meet_car_commission_coefficient;
                        $row->real_output_value = UserMonthTargetService::realOutputValue($row);


                        if ($row->positionMonthTarget->is_commission_coefficient_status == DEFAULT_STATUS_SUCCESS) {
                            $row->output_value_commission_coefficient = $row->positionMonthTarget->output_value_commission_coefficient;
                        }

                        $row->output_value_achievement_commission = UserMonthTargetService::outputValueAchievementCommission($row);
                        $row->real_output_values = $row->real_output_value;
                        $row->output_value_reach = UserMonthTargetService::outputValueReach($row);
                        $row->output_value_score = UserMonthTargetService::outputValueScore($row);
                        $row->real_maintenance_vehicle = ErpRepairSettlementService::erpRealMaintenanceVehicle([
                            'user_erp_id' => $row->user->erp_id,
                            'target_date' => $row->target_date,
                            'store_erp_id' => $row->user->store->erp_id
                        ]);

                        $row->maintenance_vehicle_reach = UserMonthTargetService::maintenanceVehicleReach($row);
                        $row->maintenance_vehicle_score = UserMonthTargetService::maintenanceVehicleScore($row);
                        $row->kpi_coefficient = $row->output_value_score + $row->maintenance_vehicle_score;
                        $row->user_month_award = UserMonthTargetService::userMonthAward($row);
                        $row->user_month_punish = UserMonthTargetService::userMonthPunish($row);
                        $row->subtotal_merit_pay = UserMonthTargetService::subtotalMeritPay($row);
                        $row->gross_profit_reach = $row->positionMonthTarget->gross_profit_reach;
                        $row->should_merit_pay = UserMonthTargetService::shouldMeritPay($row);
                        $row->basic_salary = $row->user->basic_salary;
                        $row->total_salary = $row->should_merit_pay + $row->user->basic_salary;

                        if ($row->user->probation_period_end_time && ($row->user->probation_period_end_time > $row->target_date.'-01')) {
                            $row->probation_period_salary = $row->user->probation_period_salary;
                        }
                    }

                    return $rows;
                });

                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->append('<a href="/admin/user-month-target/'.$actions->getKey().'"><i class="feather icon-eye"></i> 显示</a>');
                });

                $grid->filter(function ($filter) {
                    $filter->equal('target_date')->month();
                });

                return $grid;
            });

            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SalaryMonth(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('basic_salary');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
